package util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.StringTokenizer;

import core.Problem;
public class IO {

	public static int matrixSize;
	
	public static Problem Read(String file) {
		String string = "";
		int[][] fluxo;
		int[][] distancia;
		try {
			InputStream ips = new FileInputStream(file);
			InputStreamReader ipsr = new InputStreamReader(ips);
			BufferedReader br = new BufferedReader(ipsr);
			String line;

			ArrayList<int[][]> aMatrix = new ArrayList<int[][]>();
			Problem p = new Problem();

			// Pega o tamanho das matrizes
			// Verificar se tem um espa�o antes do n�mero
			StringTokenizer str = new StringTokenizer(br.readLine());

			p.setSize(Integer.parseInt(str.nextToken()));

			int n = p.getSize();

			matrixSize = p.getSize();

			br.readLine();
			// end

			fluxo = new int[n][n];
			distancia = new int[n][n];

			int i = 0;
			int j = 0;

			while ((line = br.readLine()) != null) {
				// Pega primeira matriz
				while (i < n) {
					StringTokenizer st = new StringTokenizer(line);
					while (j < n) {
						fluxo[i][j] = Integer.valueOf(st.nextToken());
						j++;
					}
					j = 0;
					i++;
					line = br.readLine();
				}

				// Adiciona a matriz � lista
				aMatrix.add(fluxo);
				p.setFlux(fluxo);

				// Pega segunda matriz
				i = 0;
				j = 0;

				// Se tem uma linha em branco separando as matrizes
				if (line.equals("")) {
					line = br.readLine();
				}

				while (i < n) {
					StringTokenizer st = new StringTokenizer(line);
					while (j < n) {
						distancia[i][j] = Integer.valueOf(st.nextToken());
						j++;
					}
					j = 0;
					i++;
					line = br.readLine();
				}
				// Adiciona a matriz � lista
				aMatrix.add(distancia);
				p.setDestination(distancia);
			}

			br.close();
			
			//fazer permutacao inicial
			p.makeInitialPermutation();

			return p;

		} catch (Exception e) {
			System.out.println("Erro: " + e.toString());
		}
		return null;
	}

	public static void Write(String file, String content) {
		try {
			FileWriter fw = new FileWriter(new File(file));
			BufferedWriter bw = new BufferedWriter(fw);
			PrintWriter fileOut = new PrintWriter(bw);
			fileOut.println("\n\n" + content);
			fileOut.close();
			System.out.println("O arquivo " + file + " foi criado.");
		} catch (Exception e) {
			System.out.println("Erro: " + e.toString());
		}
	}

	public static void WriteMatrix(String path, Problem content, double eval) {
		try {
			FileWriter fw = new FileWriter(new File(path), true);
			BufferedWriter bw = new BufferedWriter(fw);
			PrintWriter fileOut = new PrintWriter(bw);

			int[][] m1 = content.getFlux();
			int[][] m2 = content.getDestination();

			fileOut.println();

			// matriz 1
			for (int i = 0; i < m1.length; i++) {
				for (int j = 0; j < m1[i].length; j++) {
					fileOut.print(m1[i][j] + "\t");
				}
				fileOut.println();
			}

			fileOut.println();

			// matriz 2
			for (int i = 0; i < m2.length; i++) {
				for (int j = 0; j < m2[i].length; j++) {
					fileOut.print(m2[i][j] + "\t");
				}
				fileOut.println();
			}

			fileOut.println();

			// avaliacao
			fileOut.println(eval);

			fileOut.close();
			System.out.println("\n\nO arquivo " + path + " foi criado.");
		} catch (Exception e) {
			System.out.println("Erro: " + e.toString());
		}
	}
}
